﻿using SSC.Services;
using SSC.ViewModels;
using System.Windows;
using System.Windows.Media.Imaging;

namespace SSC.Views
{
    /// <summary>
    /// wdLogin.xaml 的交互逻辑
    /// </summary>
    public partial class wdLogin : Window
    {
        private readonly FaceRecognitionService _faceRecognitionService;
        private readonly UserService _userService;

        public wdLogin()
        {
            InitializeComponent();
            _faceRecognitionService = new FaceRecognitionService();
            _userService = new UserService();
        }

        private async void LoginButton_Click(object sender, RoutedEventArgs e)
        {
            #region 人脸识别登录方案
            /* // 捕获用户的脸部图像
             BitmapImage faceImage = _faceRecognitionService.CaptureFaceImage();
             if (faceImage == null)
             {
                 MessageBox.Show("无法捕获脸部图像，请重试。");
                 return;
             }
             // 调用人脸识别服务进行验证
             bool isFaceRecognized = await _faceRecognitionService.VerifyFaceAsync(faceImage);
             if (isFaceRecognized)
             {
                 MessageBox.Show("登录成功！");
                 // 继续进行登录后的操作
             }
             else
             {
                 MessageBox.Show("人脸识别失败，请重试。");
             }*/
            #endregion

            string username = UserName.Text.Trim();
            string password = PasswordBox.Password;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
            {
                MessageBox.Show("请输入用户名和密码！");
                return;
            }

            var user = _userService.QueryUserByName(username);
            if (user == null)
            {
                MessageBox.Show("用户不存在！");
                return;
            }
            if (user.UserPassword != password)
            {
                MessageBox.Show("密码错误！");
                return;
            }
            Variable.CurrentUser = user;
            // 记录登录操作
            var logService = new OperationLogService();
            logService.AddLog(user.UserName, "登录系统", "用户登录成功");

            MessageBox.Show("登录成功！");
            this.DialogResult = true;
            this.Close();
        }

    }
}
